Designing valuable high quality figures or illustrations takes time and energy. We can best take advantage of this investment by creating products that we can easily adjust, build upon, or reuse.
In order to do this, we need to consider how they’re composed, so that we understand what we’re working with.
Image files are almost always stored in one of two basic formats, (1) raster or (2) vector.
Raster displays (also called bitmap) like a computer screen, are composed of a rectangular grid of pixels. We fill each pixel with color, mosaic them together, and put together an image.
Vector displays are stored as lines and fills denoted by 2D points, and so an image is not subdividied into individual cells like in rasters. This allows vector images to scale better (both up and down in size).
To make sure that you never forget the difference between the two basic image types, I’ve made you a Rasta Raster of Bob Marley, where each ‘pixel’ is an image of a dog. Ta-da. Really I guess this is actually some sort of meta-raster Rasta.
Â
Because vector graphics aren’t stored as pixels, they’re more flexible to changes in size, without changing image quality. For example, if you wanted to use the same design to print a birthday card and a billboard using a raster file, you’d likely have to create low-res and hi-res versions of the same image. The size of the file scales with the quality, so your birthday card image may only be a few kilobytes, but your billboard image might be hundreds of megabytes. In contrast, a vectorized version would have the same file size and quality regardless.
Let’s see this in action:
A simple visual explanation of vector vs. raster formats. Ironically, this image is saved as a PNG (a raster format). More information is available at https://en.wikipedia.org/wiki/Vector_graphics
In addition to the convenience of size scaling, keeping our illustrations, figures, or whatever in a vector file format will assure us that we can go back and edit aspects of the original ad infinitum.
We will all now pinky-swear to export our plots/figures/et al. in vector format, preferrably as PDFs.
Start by loading a few common packages for visualization in R. If you don’t have these packages already installed, we can do that first.
install.packages("ggplot2")
install.packages("phytools")
install.packages("gridExtra")
install.packages("RColorBrewer")
install.packages("viridis")
install.packages("dplyr")
Â
Load ’em if you got ’em
library(ggplot2)
library(phytools)
library(gridExtra)
library(RColorBrewer)
library(viridis)
library(dplyr)
Â
There are a million and one color palettes available in R now. It seems like a new one pops up everyday.
Let’s have a look at some color schemes
display.brewer.all()
Roughly 4.5% of the world’s population (345 million people!) are colorblind to some extent. So maybe let’s consider colorblind friendly palettes:
display.brewer.all(colorblindFriendly = T)
For reference, the package scales has a useful function show_col() for looking at palettes.
This gives us the hex numbers (#……) which we can use to choose specific colors. Hex numbers can also come in handy if you want to identify a specific color later in Adobe Illustrator.
library(scales)
library(wesanderson)
library(colRoz)
show_col(wes_palette("Zissou1", 6, type="continuous"))
show_col(brewer.pal(6, "Paired"))
show_col(colRoz_pal("c.kingii", 6))
show_col(viridis(6))
Enough of that, let’s load and plot some data to quickly mention color and shape.
goannas <- readRDS("Goannas.RDS")
names(goannas);
## [1] "phy" "size.data" "distribution.data"
## [4] "rase.data" "geo.object"
sizes <- goannas$size.data
sizes <- mutate(sizes,
TailRatio = Tail/Body_Length)
head(sizes)
## Name_in_Tree Body_Length Group Habitat Tail TailRatio
## 1 Varanus.acanthurus 236.0 Monitor saxicolous 380 1.610169
## 2 Varanus.balagardi 280.0 Monitor arboreal 439 1.567857
## 3 Varanus.baritji 239.6 Monitor saxicolous 297 1.239566
## 4 Varanus.brevicauda 102.0 Monitor terrestrial 113 1.107843
## 5 Varanus.bushi 114.5 Monitor arboreal 159 1.388646
## 6 Varanus.caudolineatus 115.0 Monitor arboreal 135 1.173913
Do a preliminary plot of the data so we can see what we want to change.
ggplot(sizes, aes(x=log(Body_Length), y=TailRatio)) +
geom_point()
I don’t want to get too caught up here, but there are a few quick things we want to keep in mind.
We can try again, by adjusting just a few simple commands
ggplot(sizes, aes(x=log(Body_Length), y=TailRatio)) +
geom_point(size=5, alpha=0.75, aes(color=Habitat)) + theme_bw()
This is still horrifying to the eyes.
Let’s think about how color and shape can improve the plot.
YGBplot <- ggplot(sizes, aes(x=log(Body_Length), y=TailRatio)) +
geom_point(size = 5, aes(colour = Habitat)) +
scale_color_brewer(palette = "YlGnBu") +
theme_classic()
SPCplot <- ggplot(sizes, aes(x=log(Body_Length), y=TailRatio)) +
geom_point(size = 5, aes(colour = Habitat)) +
scale_color_brewer(palette = "Spectral") +
theme_classic()
REDplot <- ggplot(sizes, aes(x=log(Body_Length), y=TailRatio)) +
geom_point(size = 5, aes(colour = Habitat, shape = Habitat)) +
scale_shape_manual(values = c(15:18)) +
scale_color_brewer(palette = "RdYlBu") +
theme_classic()
BLUplot <- ggplot(sizes, aes(x=log(Body_Length), y=TailRatio)) +
geom_point(size = 5, aes(colour = Habitat, shape = Habitat)) +
scale_shape_manual(values = c(15:18)) +
scale_color_brewer(palette = "Blues") +
theme_classic()
together <- grid.arrange(SPCplot, YGBplot, BLUplot, REDplot, nrow=2)
Save this for later, and we can come back to edit it.
ggsave(filename = "Color_Comparison.pdf",
plot = together,
useDingbats = F)